import 'dart:async';

import 'package:flutter/material.dart';
import 'package:yc_product_plugin_example/ecg/charts/ecg_charts.dart';
import 'package:yc_product_plugin_example/ecg/charts/image_compostion_big_screen.dart';
import 'package:yc_product_plugin_example/ecg/charts/image_compostion_screen.dart';

class EcgResult extends StatefulWidget {
  final List ecgDatas;
  final folderName;
  const EcgResult(
      {required this.ecgDatas, required this.folderName, super.key});

  @override
  State<EcgResult> createState() => _EcgResultState();
}

class _EcgResultState extends State<EcgResult> {
  Timer? _ecgTimer;
  int index = 0;
  List ecgDatas = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    debugPrint("=====>${widget.folderName}");

    //250HZ
    int ms = (1.0 / 250.0 * 3) * 1000 ~/ 1;

    _ecgTimer ??= Timer.periodic(Duration(milliseconds: ms), (timer) {
      if (mounted) {
        setState(() {
          //datas.add(1.8341000080108643);
          // print("111");
          _addPoint();
        });
      }
    });
  }

  void _addPoint() {
    if (index < widget.ecgDatas.length) {
      ecgDatas.add(widget.ecgDatas[index]);
      index++;
      if (ecgDatas.length > 300) {
        ecgDatas = ecgDatas.sublist(ecgDatas.length - 300);
      } else {
        index = 0;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("心电图AI辅诊报告"),
      ),
      body: getBody(),
    );
  }

  Widget getBody() {
    return SingleChildScrollView(
      child: Container(
        height: 2000,
        child: Column(
          children: [
            Container(
                height: 300,
                padding: EdgeInsets.all(8),
                child: CustomPaint(
                  size: Size(300, 300),
                  painter: ECGPainter(datas: ecgDatas),
                )),
            Container(
              child: Column(
                children: [
                  Text("诊断结果"),
                  Text("正常心电图"),
                  Text("详情：详情：QRS波形形态时限振幅正常，P-R间期正常，ST-T无改变，Q-T间期正常。"),
                  Container(
                    child: Text("负荷指数"),
                  ),
                  Container(
                    child: Text("HRV指数"),
                  ),
                  Container(
                    child: Text("压力指数"),
                  ),
                ],
              ),
            ),
            GestureDetector(
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => ImageCompositionBigScreen(
                          folderName: widget.folderName)),
                );
              },
              child: Container(
                height: 138,
                child: ImageCompositionScreen(folderName: widget.folderName),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
